$(function () {
	
    $("#jqGrid").jqGrid({
        url: baseURL + 'app/user/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', width: 30, key: true ,hidden : true},
			{ label: '姓名', name: 'name', sortable: false, width: 30 },
			{ label: '姓名', name: 'name', sortable: false, width: 30 },
			{ label: '性别', name: 'gender', sortable: false, width: 30 },
			{ label: '状态', name: 'status', sortable: false, width: 30 },
			{ label: '手机号码', name: 'mobile', sortable: false, width: 30 },
			{ label: '更新时间', name: 'updateTime', sortable: false, width: 30 },
			
			{ label: '签名', name:'qm',index:'qm',align:"center" ,sortable:false,editable: true,width:30,formatter:imageFormat}
			//{ label: 'pdf路径', name: 'pathPdf', width: 100 },
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
    function imageFormat(cellvalue,options,rowObject ){
    	if(cellvalue == null){
    		return "";
    	}
    	
    	var src = 'http://129.204.27.177:4869/'+cellvalue;
        return "<img src='"+src+"' style='width:50px;height:50px;' />";
    }
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            paramKey: null
		},
		showList: true,
		showPdf: false,
		title: null,
		config: {},
		pdf: null,
		qm: null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.config = {};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			$.get(baseURL + "app/user/info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改签名";
                vm.config = r.config;
                console.log(r.config.qm);
                $('#imgQm').attr("src",r.url+r.config.qm);
                
                new AjaxUpload('#upload', {
			        action: baseURL + "app/user/upload",
			        name: 'file',
			        autoSubmit:true,
			        data : {
			            'id' : id
			        },
			        responseType:"json",
			        onSubmit:function(file, extension){
			            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
			                alert('只支持jpg、png、gif格式的图片！');
			                return false;
			            }
			        },
			        onComplete : function(file, r){
			        	alert(r.msg);
			            if(r.code == 0){
			            	$('#imgQm').attr("src",r.url);
			            	vm.qm = r.url
			                //vm.reload();
			            	window.location.reload();
			            }
			        }
			    });
            });
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "app/user/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function (event) {
			var url = vm.config.id == null ? "app/user/save" : "app/user/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.config),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			vm.showPdf = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		}
	}
});